package com.innovation.chengzhangriji.http;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.innovation.library.http.HeadInfo;
import com.innovation.library.http.InnovationClient;
import com.innovation.library.http.InnovationHttpResponseHandler;
import com.innovation.library.http.InnovationRequestBody;
import com.jiuan.oa.android.library.util.Installation;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.FileAsyncHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.orhanobut.logger.Logger;

import org.apache.http.client.params.ClientPNames;
import org.apache.http.entity.InputStreamEntity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Client {

    /*根据通讯协议设定的常量*/

    /**
     * 备孕
     */
    public static final int STATE_READY = 1;
    /**
     * 怀孕
     */
    public static final int STATE_START = 2;
    /**
     * 出生
     */
    public static final int STATE_END = 3;
    /**
     * 男孩
     */
    public static final int BOY = 1;
    /**
     * 女孩
     */
    public static final int GIRL = 2;
    /**
     * 未设定
     */
    public static final int UNKNOWN = -1;

    /**
     * 空的时间戳
     */
    public static final long EMPTY_TIME = -2208988800l;

    private static final String OS = "Android";

    private static final String APP = "BabyDiary";

    public interface NetworkUnavailableListener {
        void networkUnavailable();
    }

    private Client() {
    }

    /*1*/

    /**
     * 注册获取验证码
     *
     * @param phoneNumber 手机号
     */
    public static RequestHandle requestSignUpGetCode(Context context, String phoneNumber, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        SignUpGetCodeRequest helper = new SignUpGetCodeRequest();
        helper.setPhoneNum(phoneNumber);
        return request(context, helper, SignUpGetCodeRequest.PATH, responseHandler, listener);
    }

    /*2*/

    /**
     * 注册检查验证码
     *
     * @param phoneNumber 手机号
     * @param code        验证码
     */
    public static RequestHandle requestSignUpCheckCode(Context context, String phoneNumber, String code, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        SignUpCheckCodeRequest helper = new SignUpCheckCodeRequest();
        helper.setPhone(phoneNumber);
        helper.setCode(code);
        return request(context, helper, SignUpCheckCodeRequest.PATH, responseHandler, listener);
    }

    /*3*/

    /**
     * 注册
     *
     * @param phoneNumber 手机号
     * @param code        验证码
     * @param password    密码
     */
    public static RequestHandle requestSignUp(Context context, String phoneNumber, String code, String password, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        SignUpRequest helper = new SignUpRequest();
        helper.setPhoneNum(phoneNumber);
        helper.setCode(code);
        helper.setPassword(password);
        return request(context, helper, SignUpRequest.PATH, responseHandler, listener);
    }

    /*4*/

    /**
     * 登录
     *
     * @param phoneNumber 手机号
     * @param password    密码
     */
    public static RequestHandle requestLogin(Context context, String phoneNumber, String password, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        LoginRequest helper = new LoginRequest();
        helper.setPhoneNum(phoneNumber);
        helper.setPassword(password);
        return request(context, helper, LoginRequest.PATH, responseHandler, listener);
    }

    /*5*/

    /**
     * 申请重置密码验证码
     *
     * @param phoneNumber 手机号
     */
    public static RequestHandle requestResetGetCode(Context context, String phoneNumber, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        ResetGetCodeRequest helper = new ResetGetCodeRequest();
        helper.setPhoneNum(phoneNumber);
        return request(context, helper, ResetGetCodeRequest.PATH, responseHandler, listener);
    }

    /*6*/

    /**
     * 验证重置密码验证码
     *
     * @param phoneNumber 手机号
     * @param code        验证码
     */
    public static RequestHandle requestResetCheckCode(Context context, String phoneNumber, String code, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        ResetCheckCodeRequest helper = new ResetCheckCodeRequest();
        helper.setPhoneNum(phoneNumber);
        helper.setCode(code);
        return request(context, helper, ResetCheckCodeRequest.PATH, responseHandler, listener);
    }

    /*7*/

    /**
     * 重置密码
     *
     * @param phoneNumber 手机号
     * @param code        验证码
     * @param password    密码
     */
    public static RequestHandle requestReset(Context context, String phoneNumber, String code, String password, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        ResetRequest helper = new ResetRequest();
        helper.setPhoneNum(phoneNumber);
        helper.setCode(code);
        helper.setPassword(password);
        return request(context, helper, ResetRequest.PATH, responseHandler, listener);
    }

    /*8*/

    /**
     * 访问令牌刷新
     *
     * @param phoneNumber  手机号
     * @param refreshToken 令牌
     */
    public static RequestHandle requestRefreshToken(Context context, String phoneNumber, String refreshToken, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        RefreshTokenRequest helper = new RefreshTokenRequest();
        helper.setPhoneNum(phoneNumber);
        helper.setToken(refreshToken);
        return request(context, helper, RefreshTokenRequest.PATH, responseHandler, listener);
    }

    /*9*/

    /**
     * 申请手机动态登录码
     *
     * @param phoneNumber 手机号
     */
    public static RequestHandle requestDynamicLoginGetCode(Context context, String phoneNumber, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        DynamicLoginGetCodeRequest helper = new DynamicLoginGetCodeRequest();
        helper.setPhoneNum(phoneNumber);
        return request(context, helper, DynamicLoginGetCodeRequest.PATH, responseHandler, listener);
    }

    /*10*/

    /**
     * 手机动态码登录
     *
     * @param phoneNumber 手机号
     * @param code        验证码
     */
    public static RequestHandle requestDynamicLogin(Context context, String phoneNumber, String code, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        DynamicLoginRequest helper = new DynamicLoginRequest();
        helper.setPhoneNum(phoneNumber);
        helper.setCode(code);
        return request(context, helper, DynamicLoginRequest.PATH, responseHandler, listener);
    }

    /*11*/

    /**
     * 获取用户个人信息资料
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     */
    public static RequestHandle requestAccountGet(Context context, String phoneNumber, String accessToken, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        AccountGetRequest helper = new AccountGetRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        return request(context, helper, AccountGetRequest.PATH, responseHandler, listener);
    }

    /*12*/

    /**
     * 修改用户个人信息资料
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param changes     UserName(用户昵称);
     *                    Sex(性别);
     *                    Birthday(生日);
     */
    public static RequestHandle requestAccountUpdate(Context context, String phoneNumber, String accessToken, Change[] changes, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        AccountUpdateRequest helper = new AccountUpdateRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setChanges(changes);
        return request(context, helper, AccountUpdateRequest.PATH, responseHandler, listener);
    }

    /*13*/

    /**
     * 修改密码
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param passwordOld 旧密码
     * @param passwordNew 新密码
     */
    public static RequestHandle requestPasswordUpdate(Context context, String phoneNumber, String accessToken, String passwordOld, String passwordNew, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        PasswordUpdateRequest helper = new PasswordUpdateRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setPasswordOld(passwordOld);
        helper.setPasswordNew(passwordNew);
        Logger.d(helper.getContent());
        return request(context, helper, PasswordUpdateRequest.PATH, responseHandler, listener);
    }

    /*14*/

    /**
     * 获取当前用户所有宝宝信息
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     */
    public static RequestHandle requestAllBaby(Context context, String phoneNumber, String accessToken, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        AllBabyRequest helper = new AllBabyRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        Logger.d(helper.getContent());
        return request(context, helper, AllBabyRequest.PATH, responseHandler, listener);
    }

    /*15*/

    /**
     * 获取单个宝宝信息
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param id          宝宝标识
     * @param updateTime  更新时间
     */
    public static RequestHandle requestBaby(Context context, String phoneNumber, String accessToken, int id, long updateTime, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        BabyRequest helper = new BabyRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setId(id);
        helper.setUpdateTime(updateTime);
        return request(context, helper, BabyRequest.PATH, responseHandler, listener);
    }

    /*16*/

    /**
     * 添加宝宝信息
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param changes     NickName(宝宝昵称);
     *                    GrowthStageID(成长阶段)(1备孕中2怀孕期3宝宝出生);
     *                    UserMemberName(称谓);
     *                    LogoPath(头像图片地址);
     *                    Sex(性别)(1男2女-1空);
     *                    Birthday(生日);
     *                    LastMenstrualPeriod(末次月经日期);
     *                    ExpectedDateChildbirth(预产期);
     */
    public static RequestHandle requestBabyAdd(Context context, String phoneNumber, String accessToken, Change[] changes, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        BabyAddRequest helper = new BabyAddRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setChanges(changes);
        return request(context, helper, BabyAddRequest.PATH, responseHandler, listener);
    }

    /*17*/

    /**
     * 更新宝宝信息
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param id          宝宝标识
     * @param updateTime  更新时间
     * @param changes     NickName(宝宝昵称);
     *                    UserMemberName(称谓);
     *                    LogoPath(头像图片地址);
     *                    Sex(性别)(1男2女-1空);
     *                    Birthday(生日);
     *                    LastMenstrualPeriod(末次月经日期);
     *                    ExpectedDateChildbirth(预产期);
     */
    public static RequestHandle requestBabyUpdate(Context context, String phoneNumber, String accessToken, int id, long updateTime, Change[] changes, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        BabyUpdateRequest helper = new BabyUpdateRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setId(id);
        helper.setUpdateTime(updateTime);
        helper.setChanges(changes);
        return request(context, helper, BabyUpdateRequest.PATH, responseHandler, listener);
    }

    /*18*/

    /**
     * 通用上传图片
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param ext         后缀名
     * @param data        图片文件字节流的Base64编码字符串
     */
    public static RequestHandle requestPictureResourceUpload(Context context, String phoneNumber, String accessToken, String ext, String data, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        PictureResourceUploadRequest helper = new PictureResourceUploadRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setExt(ext);
        helper.setData(data);
        return request(context, helper, PictureResourceUploadRequest.PATH, responseHandler, listener);
    }

    /*19*/

    /**
     * 修改宝宝头像
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param id          宝宝标识
     * @param ext         后缀名
     * @param data        图片文件字节流的Base64编码字符串
     * @param updateTime  更新时间
     */
    public static RequestHandle requestBabyPhotoUpdate(Context context, String phoneNumber, String accessToken, int id, String ext, String data, long updateTime, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        BabyPhotoUpdateRequest helper = new BabyPhotoUpdateRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setId(id);
        helper.setExt(ext);
        helper.setData(data);
        helper.setUpdateTime(updateTime);
        return request(context, helper, BabyPhotoUpdateRequest.PATH, responseHandler, listener);
    }

    /*20*/

    /**
     * 获取宝宝二维码
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param id          宝宝标识
     */
    public static RequestHandle requestBabyQRCodeGet(Context context, String phoneNumber, String accessToken, int id, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        BabyQRCodeGetRequest helper = new BabyQRCodeGetRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setId(id);
        return request(context, helper, BabyQRCodeGetRequest.PATH, responseHandler, listener);
    }

    /*21*/

    /**
     * 应用宝宝二维码
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param member      称谓
     * @param qrCode      扫描二维码后解析出的字符串(内容为guid形式)
     */
    public static RequestHandle requestBabyQRCode(Context context, String phoneNumber, String accessToken, String member, String qrCode, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        BabyQRCodeRequest helper = new BabyQRCodeRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setMember(member);
        helper.setQrCode(qrCode);
        return request(context, helper, BabyQRCodeRequest.PATH, responseHandler, listener);
    }

    /*22*/

    /**
     * 获取所有里程碑信息
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     */
    public static RequestHandle requestMilestoneGet(Context context, String phoneNumber, String accessToken, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        MilestoneGetRequest helper = new MilestoneGetRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        return request(context, helper, MilestoneGetRequest.PATH, responseHandler, listener);
    }

    /*23*/

    /**
     * 获取宝宝日记总数
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param id          宝宝标识
     */
    public static RequestHandle requestCountGet(Context context, String phoneNumber, String accessToken, int id, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        CountGetRequest helper = new CountGetRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setId(id);
        return request(context, helper, CountGetRequest.PATH, responseHandler, listener);
    }

    /*24*/

    /**
     * 获取图片主题
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     */
    public static RequestHandle requestThemeGet(Context context, String phoneNumber, String accessToken, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        ThemeGetRequest helper = new ThemeGetRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        return request(context, helper, ThemeGetRequest.PATH, responseHandler, listener);
    }

    /*25*/

    /**
     * 发表文字日记
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param uuid        唯一标识
     * @param babyID      宝宝标识
     * @param text        文字内容
     * @param label       里程碑
     * @param labelId     里程碑标识
     * @param localTime   本地创建时间
     * @param state       宝贝阶段
     */
    public static RequestHandle requestTextUpload(
            Context context, String phoneNumber, String accessToken, String uuid, int babyID, String text, String label, int labelId,
            long localTime, int state, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        TextUploadRequest helper = new TextUploadRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setUuid(uuid);
        helper.setBabyID(babyID);
        helper.setText(text);
        helper.setLabel(label);
        helper.setLabelId(labelId);
        helper.setCreateTS(localTime);
        helper.setState(state);
        return request(context, helper, TextUploadRequest.PATH, responseHandler, listener);
    }

    /*26*/

    /**
     * 发表相册日记
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param uuid        唯一标识
     * @param babyID      宝宝标识
     * @param text        文字内容
     * @param theme       图片主题
     * @param urls        图片路径
     * @param label       里程碑
     * @param labelId     里程碑标识
     * @param localTime   本地创建时间
     * @param state       宝贝阶段
     */
    public static RequestHandle requestPhotoUpload(
            Context context, String phoneNumber, String accessToken, String uuid, int babyID, String text, String theme, String[] urls, String label, int labelId,
            long localTime, int state, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        PhotoUploadRequest helper = new PhotoUploadRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setUuid(uuid);
        helper.setBabyID(babyID);
        helper.setTheme(theme);
        helper.setText(text);
        helper.setUrls(urls);
        helper.setLabel(label);
        helper.setLabelId(labelId);
        helper.setCreateTS(localTime);
        helper.setState(state);
        return request(context, helper, PhotoUploadRequest.PATH, responseHandler, listener);
    }

    /*27*/

    /**
     * 音频资源上传
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param ext         后缀名
     * @param data        音频文件字节流的Base64编码字符串
     */
    public static RequestHandle requestAudioResourceUpload(Context context, String phoneNumber, String accessToken, String ext, String data, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        AudioResourceUploadRequest helper = new AudioResourceUploadRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setExt(ext);
        helper.setData(data);
        return request(context, helper, AudioResourceUploadRequest.PATH, responseHandler, listener);
    }

    /*28*/

    /**
     * 发表音频日记
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param uuid        唯一标识
     * @param babyID      宝宝标识
     * @param length      音频长度
     * @param audioUrl    音频地址
     * @param createTS    本地创建时间
     * @param state       宝贝阶段
     */
    public static RequestHandle requestAudioUpload(Context context, String phoneNumber, String accessToken, String uuid, int babyID, int length, String audioUrl, long createTS, int state, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        AudioUploadRequest helper = new AudioUploadRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setUuid(uuid);
        helper.setBabyID(babyID);
        helper.setLength(length);
        helper.setAudioUrl(audioUrl);
        helper.setCreateTS(createTS);
        helper.setState(state);
        return request(context, helper, AudioUploadRequest.PATH, responseHandler, listener);
    }

    /*29*/

    /**
     * 视频资源上传
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param ext         后缀名
     * @param data        视频文件字节流的Base64编码字符串
     */
    public static RequestHandle requestVideoResourceUpload(Context context, String phoneNumber, String accessToken, String ext, String data, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        VideoResourceUploadRequest helper = new VideoResourceUploadRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setExt(ext);
        helper.setData(data);
        Log.d("视频", "" + helper.getContent());
        return request(context, helper, VideoResourceUploadRequest.PATH, responseHandler, listener);
    }

    /*30*/

    /**
     * 发表视频日记
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param uuid        唯一标识
     * @param id          宝宝标识
     * @param length      视频长度
     * @param videoUrl    视频地址
     * @param pictureUrl  截图地址
     * @param localTime   本地创建时间
     * @param state       宝贝阶段
     */
    public static RequestHandle requestVideoUpload(Context context, String phoneNumber, String accessToken, String uuid, int id, int length, String videoUrl, String pictureUrl, long localTime, int state, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        VideoUploadRequest helper = new VideoUploadRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setUuid(uuid);
        helper.setBabyID(id);
        helper.setLength(length);
        helper.setVideoUrl(videoUrl);
        helper.setPictureUrl(pictureUrl);
        helper.setCreateTS(localTime);
        helper.setState(state);
        return request(context, helper, VideoUploadRequest.PATH, responseHandler, listener);
    }

    /*31*/

    /**
     * 请求同步数据
     *
     * @param phone       手机号
     * @param accessToken 令牌
     * @param babyID      宝宝标识
     * @param length      请求长度
     * @param syncTS      同步时间戳
     */
    public static RequestHandle requestSyncData(Context context, String phone, String accessToken, int babyID, int length, long syncTS, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        SyncDataRequest helper = new SyncDataRequest();
        helper.setPhone(phone);
        helper.setToken(accessToken);
        helper.setId(babyID);
        helper.setLength(length);
        helper.setServerTime(syncTS);
        return request(context, helper, SyncDataRequest.PATH, responseHandler, listener);
    }

    /*32*/

    /**
     * 请求同步剩余数据
     *
     * @param phone       手机号
     * @param accessToken 令牌
     * @param babyID      宝宝标识
     * @param length      请求长度
     * @param syncTS      同步时间戳
     * @param createTS    创建时间戳
     */
    public static RequestHandle requestSyncLastData(Context context, String phone, String accessToken, int babyID, int length, long syncTS, long createTS, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        SyncLastDataRequest helper = new SyncLastDataRequest();
        helper.setPhone(phone);
        helper.setToken(accessToken);
        helper.setId(babyID);
        helper.setLength(length);
        helper.setServerTime(syncTS);
        helper.setCreateTS(createTS);
        return request(context, helper, SyncLastDataRequest.PATH, responseHandler, listener);
    }

    /*33*/

    /**
     * 删除指定日记
     *
     * @param phoneNumber 手机号
     * @param accessToken 令牌
     * @param uuid        唯一标识
     * @param deleteTS    创建时间
     */
    public static RequestHandle requestDataDelete(Context context, String phoneNumber, String accessToken, String uuid, long deleteTS, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        DataDeleteRequest helper = new DataDeleteRequest();
        helper.setPhone(phoneNumber);
        helper.setToken(accessToken);
        helper.setUuid(uuid);
        helper.setDeleteTS(deleteTS);
        return request(context, helper, DataDeleteRequest.PATH, responseHandler, listener);
    }

    /*34*/

    /**
     * 申请上传票据
     *
     * @param phone       手机号
     * @param accessToken 令牌
     * @param type        上传类型 {@link TicketGetRequest#TYPE_IMAGE};{@link TicketGetRequest#TYPE_AUDIO};{@link TicketGetRequest#TYPE_VIDEO};
     * @param ext         后缀名
     * @param number      分块数量
     */
    public static RequestHandle requestTicketGet(Context context, String phone, String accessToken, int type, String ext, int number, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        TicketGetRequest helper = new TicketGetRequest();
        helper.setPhone(phone);
        helper.setToken(accessToken);
        helper.setType(type);
        helper.setExt(ext);
        helper.setNumber(number);
        return request(context, helper, TicketGetRequest.PATH, responseHandler, listener);
    }

    /*35*/

    /**
     * 上传文件
     *
     * @param ticket 票据
     * @param number 分块数量
     * @param file   文件
     */
    public static RequestHandle requestFileUpload(Context context, String ticket, int number, File file, AsyncHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {

        if (!isNetworkConnect(context)) {
            if (listener != null) {
                listener.networkUnavailable();
            }
            return null;
        }

        InnovationClient client = InnovationClient.getInstance();
        RequestParams params = new RequestParams();
        params.put("SC", BaseRequestConfig.SC);
        params.put("SV", BaseRequestConfig.SV_MULTI_FILE_UPLOAD);
        params.put("Ticket", ticket);
        params.put("SeqID", number);
        params.put(HeadInfo.APP, APP);
        String url = AsyncHttpClient.getUrlWithQueryString(false, BaseRequest.PATH + "api/large_file_block", params);
        try {
            InputStreamEntity entity = new InputStreamEntity(new FileInputStream(file), -1);
            entity.setContentType("application/octet-stream");
            entity.setChunked(true);
            return client.post(context, url, entity, responseHandler);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*38*/

    public static RequestHandle requestHistory(Context context, String phone, String accessToken, int babyID, long createTS, int length, int type, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        HistoryRequest helper = new HistoryRequest();
        helper.setPhone(phone);
        helper.setToken(accessToken);
        helper.setBabyID(babyID);
        helper.setCreateTS(createTS);
        helper.setLength(length);
        helper.setType(type);
        return request(context, helper, HistoryRequest.PATH, responseHandler, listener);
    }

    /*39*/

    /**
     * @param phone       手机号
     * @param accessToken 令牌
     * @param type        上传类型 {@link TicketGetRequest#TYPE_IMAGE};{@link TicketGetRequest#TYPE_AUDIO};{@link TicketGetRequest#TYPE_VIDEO};
     * @param ext         后缀名
     * @param file        文件
     */
    public static RequestHandle requestFileUpload(Context context, String phone, String accessToken, int type, String ext, File file, AsyncHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        if (!isNetworkConnect(context)) {
            if (listener != null) {
                listener.networkUnavailable();
            }
            return null;
        }

        InnovationClient client = InnovationClient.getInstance();
        RequestParams params = new RequestParams();
        params.put("SC", BaseRequestConfig.SC);
        params.put("SV", BaseRequestConfig.SV_FILE_UPLOAD);
        params.put("UN", phone);
        params.put("AccessToken", accessToken);
        params.put("Type", type);
        params.put("Ext", ext);
        params.put(HeadInfo.APP, APP);
        String url = AsyncHttpClient.getUrlWithQueryString(false, BaseRequest.PATH + "api/large_file_single", params);
        try {
            InputStreamEntity entity = new InputStreamEntity(new FileInputStream(file), -1);
            entity.setContentType("application/octet-stream");
            entity.setChunked(true);
            return client.post(context, url, entity, responseHandler);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*41*/

    /**
     * @param phone  手机号
     * @param token  令牌
     * @param babyID 宝贝ID
     */
    public static RequestHandle requestRemoveNote(Context context, String phone, String token, int babyID, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        RemoveNoteRequest helper = new RemoveNoteRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setBabyID(babyID);
        return request(context, helper, RemoveNoteRequest.PATH, responseHandler, listener);
    }

    /*42*/

    /**
     * @param phone       手机号
     * @param token       令牌
     * @param babyID      宝贝ID
     * @param invitePhone 邀请手机号
     * @param inviteName  邀请称谓
     */
    public static RequestHandle requestInvite(Context context, String phone, String token, int babyID, String invitePhone, String inviteName, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        InviteRequest helper = new InviteRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setBabyID(babyID);
        helper.setInvitePhone(invitePhone);
        helper.setInviteName(inviteName);
        return request(context, helper, InviteRequest.PATH, responseHandler, listener);
    }

    /*43*/

    /**
     * @param phone  手机号
     * @param token  令牌
     * @param babyID 宝贝ID
     * @param role   称谓
     */
    public static RequestHandle requestJoin(Context context, String phone, String token, int babyID, String role, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        JoinRequest helper = new JoinRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setBabyID(babyID);
        helper.setRole(role);
        return request(context, helper, JoinRequest.PATH, responseHandler, listener);
    }

    /*44*/

    /**
     * @param phone      手机号
     * @param token      令牌
     * @param babyID     宝贝ID
     * @param applyID    申请ID
     * @param applyState 申请审核状态
     */
    public static RequestHandle requestCheckJoin(Context context, String phone, String token, int babyID, String applyID, int applyState, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        CheckJoinRequest helper = new CheckJoinRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setBabyID(babyID);
        helper.setApplyID(applyID);
        helper.setApplyState(applyState);
        return request(context, helper, CheckJoinRequest.PATH, responseHandler, listener);
    }

    /*45*/

    /**
     * @param phone   手机号
     * @param token   令牌
     * @param babyID  宝宝ID
     * @param severTS 服务器时间戳
     */
    public static RequestHandle requestNewMessage(Context context, String phone, String token, int babyID, long severTS, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        NewMessageRequest helper = new NewMessageRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setBabyID(babyID);
        helper.setServerTS(severTS);
        return request(context, helper, NewMessageRequest.PATH, responseHandler, listener);
    }

    /*46*/

    /**
     * @param phone       手机号
     * @param token       令牌
     * @param searchPhone 搜索的手机号
     */
    public static RequestHandle requestSearchAllBaby(Context context, String phone, String token, String searchPhone, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        SearchAllBabyRequest helper = new SearchAllBabyRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setSearchPhone(searchPhone);
        return request(context, helper, SearchAllBabyRequest.PATH, responseHandler, listener);
    }

    /*47*/

    /**
     * @param phone       手机号
     * @param accessToken 令牌
     * @param babyID      宝宝ID
     */
    public static RequestHandle requestBabyAll(Context context, String phone, String accessToken, int babyID, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        BabyAllRequest helper = new BabyAllRequest();
        helper.setPhone(phone);
        helper.setToken(accessToken);
        helper.setBabyID(babyID);
        return request(context, helper, BabyAllRequest.PATH, responseHandler, listener);
    }

    /*50*/

    /**
     * @param phone       手机号
     * @param accessToken 令牌
     * @param userId      关注人ID
     */
    public static RequestHandle requestCancel(Context context, String phone, String accessToken, int babyId, int userId, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        CancelRequest helper = new CancelRequest();
        helper.setPhone(phone);
        helper.setToken(accessToken);
        helper.setBabyId(babyId);
        helper.setUserId(userId);
        return request(context, helper, CancelRequest.PATH, responseHandler, listener);
    }

    /*51*/

    /**
     * @param phone       手机号
     * @param accessToken 令牌
     * @param babyId      宝宝ID
     * @param userId      用户ID
     * @param role        身份
     * @param permission  读写权限
     * @param range       查看范围
     */
    public static RequestHandle requestSetPermission(Context context, String phone, String accessToken, int babyId, int userId, int role, int permission, int range, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        SetPermissionRequest helper = new SetPermissionRequest();
        helper.setPhone(phone);
        helper.setToken(accessToken);
        helper.setBabyId(babyId);
        helper.setUserId(userId);
        helper.setRole(role);
        helper.setPermission(permission);
        helper.setRange(range);
        return request(context, helper, SetPermissionRequest.PATH, responseHandler, listener);
    }

    /*52*/

    /**
     * @param phone       手机号
     * @param accessToken 令牌
     * @param babyId      宝宝ID
     * @param userId      用户ID
     */
    public static RequestHandle requestChangePermission(Context context, String phone, String accessToken, int babyId, int userId, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        ChangePermissionRequest helper = new ChangePermissionRequest();
        helper.setPhone(phone);
        helper.setToken(accessToken);
        helper.setBabyId(babyId);
        helper.setUserId(userId);
        return request(context, helper, ChangePermissionRequest.PATH, responseHandler, listener);
    }

    /*53*/

    /**
     * @param phone       手机号
     * @param accessToken 令牌
     * @param invitePhone 邀请手机号
     * @param babyId      宝宝ID
     * @param member      称谓
     */
    public static RequestHandle requestAdminInvite(Context context, String phone, String accessToken, String invitePhone, int babyId, String member, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        AdminInviteRequest helper = new AdminInviteRequest();
        helper.setPhone(phone);
        helper.setToken(accessToken);
        helper.setInvitePhone(invitePhone);
        helper.setBabyId(babyId);
        helper.setMember(member);
        return request(context, helper, AdminInviteRequest.PATH, responseHandler, listener);
    }

    /*54*/

    /**
     * @param phone       手机号
     * @param accessToken 令牌
     * @param inviteId    邀请ID
     * @param babyId      宝宝ID
     * @param inviteState 邀请状态
     */
    public static RequestHandle requestAdminInviteComplete(Context context, String phone, String accessToken, int inviteId, int babyId, int inviteState, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        AdminInviteCompleteRequest helper = new AdminInviteCompleteRequest();
        helper.setPhone(phone);
        helper.setToken(accessToken);
        helper.setInviteId(inviteId);
        helper.setBabyId(babyId);
        helper.setInviteState(inviteState);
        return request(context, helper, AdminInviteCompleteRequest.PATH, responseHandler, listener);
    }

    /*55*/

    /**
     * @param phone       手机号
     * @param accessToken 令牌
     * @param inviteId    邀请ID
     * @param babyId      宝宝ID
     * @param inviteState 邀请状态
     */
    public static RequestHandle requestInviteComplete(Context context, String phone, String accessToken, int inviteId, int babyId, int inviteState, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        InviteCompleteRequest helper = new InviteCompleteRequest();
        helper.setPhone(phone);
        helper.setToken(accessToken);
        helper.setInviteId(inviteId);
        helper.setBabyId(babyId);
        helper.setInviteState(inviteState);
        return request(context, helper, InviteCompleteRequest.PATH, responseHandler, listener);
    }

    /*57*/

    /**
     * 检测升级
     *
     * @param context         上下文
     * @param packageName     包名
     * @param version         版本
     * @param channel         渠道
     * @param responseHandler 响应处理
     * @param listener        网络未连接接口
     * @return 处理
     */
    public static RequestHandle requestCheckVersion(Context context, String packageName, String version, String channel, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        CheckVersionRequest helper = new CheckVersionRequest();
        helper.setPackageName(packageName);
        helper.setVersion(version);
        helper.setChannel(channel);
        Logger.d(helper.getContent());
        return request(context, helper, CheckVersionRequest.PATH, responseHandler, listener);
    }

    /*58*/

    /**
     * @param phone    手机号
     * @param token    令牌
     * @param password 密码
     */
    public static RequestHandle requestCheckPassword(Context context, String phone, String token, String password, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        CheckPasswordRequest helper = new CheckPasswordRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setPassword(password);
        return request(context, helper, CheckPasswordRequest.PATH, responseHandler, listener);
    }

    /**
     * @param phone      手机号
     * @param token      令牌
     * @param diaryId    日记id
     * @param uuid       评论id
     * @param comment    评论内容
     * @param targetUuid 评论回复id
     */
    public static RequestHandle requestComment(Context context, String phone, String token, String diaryId, String uuid, String comment, String targetUuid, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        CommentRequest helper = new CommentRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setDiaryId(diaryId);
        helper.setUuid(uuid);
        helper.setComment(comment);
        if (!TextUtils.isEmpty(targetUuid)) {
            helper.setTargetUuid(targetUuid);
        }
        Logger.json(helper.getContent());
        return request(context, helper, CommentRequest.PATH, responseHandler, listener);
    }

    /*59*/

    /**
     * @param phone 手机号
     * @param token 令牌
     * @param uuid  评论id
     */
    public static RequestHandle requestDeleteComment(Context context, String phone, String token, String uuid, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        DeleteCommentRequest helper = new DeleteCommentRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setUuid(uuid);
        return request(context, helper, DeleteCommentRequest.PATH, responseHandler, listener);
    }

    /*60*/

    /**
     * @param phone   手机号
     * @param token   令牌
     * @param diaryId 日记id
     * @param uuid    评论id
     */
    public static RequestHandle requestLike(Context context, String phone, String token, String diaryId, String uuid, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        LikeRequest helper = new LikeRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setDiaryId(diaryId);
        helper.setUuid(uuid);
        return request(context, helper, LikeRequest.PATH, responseHandler, listener);
    }

    /*61*/

    /**
     * @param phone 手机号
     * @param token 令牌
     * @param uuid  评论id
     */
    public static RequestHandle requestUnLike(Context context, String phone, String token, String diaryId, String uuid, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        UnLikeRequest helper = new UnLikeRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setDiaryId(diaryId);
        helper.setUuid(uuid);
        return request(context, helper, UnLikeRequest.PATH, responseHandler, listener);
    }

    /*62*/

    /**
     * @param phone  手机号
     * @param token  令牌
     * @param ts     时间戳
     * @param length 分页长度
     */
    public static RequestHandle requestDiscover(Context context, String phone, String token, long ts, int length, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        DiscoverRequest helper = new DiscoverRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setTs(ts);
        helper.setLength(length);
        return request(context, helper, DiscoverRequest.PATH, responseHandler, listener);
    }

    /*63*/

    /**
     * @param phone  手机号
     * @param token  令牌
     * @param ts     时间戳
     * @param length 分页长度
     */
    public static RequestHandle requestLoadDiscover(Context context, String phone, String token, long ts, int length, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        DiscoverLoadRequest helper = new DiscoverLoadRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setTs(ts);
        helper.setLength(length);
        return request(context, helper, DiscoverLoadRequest.PATH, responseHandler, listener);
    }

    /*64*/

    /**
     * 根据日记编号获取单个日记及评论信息
     * @param phone     手机号
     * @param token     令牌
     * @param diaryId   日记编号
     * @return
     */
    public static RequestHandle requestSingleDiscover(Context context, String phone, String token, String diaryId, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        DiscoverSingleRequest request = new DiscoverSingleRequest(diaryId);
        request.setPhone(phone);
        request.setToken(token);
        return request(context, request, DiscoverSingleRequest.PATH, responseHandler, listener);
    }

    /*65*/

    /**
     * @param phone 手机号
     * @param token 令牌
     */
    public static RequestHandle requestDiscoverComment(Context context, String phone, String token, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        DiscoverCommentRequest helper = new DiscoverCommentRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        return request(context, helper, DiscoverCommentRequest.PATH, responseHandler, listener);
    }

    /*67*/

    public static RequestHandle requestPushToken(Context context, String phone, String token, String pushToken, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        PushTokenRequest helper = new PushTokenRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        helper.setDeviceId(telephonyManager.getDeviceId());
        helper.setAppId(Installation.id(context));
        helper.setPushToken(pushToken);
        return request(context, helper, PushTokenRequest.PATH, responseHandler, listener);
    }

    /*68*/

    public static RequestHandle requestConnectDevice(Context context, String phone, String token, String deviceId, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        ConnectDeviceRequest helper = new ConnectDeviceRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setDeviceId(deviceId);
        return request(context, helper, ConnectDeviceRequest.PATH, responseHandler, listener);
    }

    /**
     * 69 获取历史头像
     */
    public static RequestHandle requestHistoryAvatar(Context context, String phone, String token, String babyId, int size, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        HistoryAvatarRequest helper = new HistoryAvatarRequest();
        helper.setPhone(phone);
        helper.setToken(token);
        helper.setSize(size);
        helper.setBabyId(babyId);
        return request(context, helper, HistoryAvatarRequest.PATH, responseHandler, listener);
    }

    /**
     * 70 标记推送消息全部已读
     */
    public static RequestHandle requestAllPushRead(Context context, String phone, String token, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        final BaseTokenRequestBodyImp body = new BaseTokenRequestBodyImp(BaseRequestConfig.SV_ALL_PUSH_READ);
        body.setPhone(phone);
        body.setToken(token);
        BaseTokenRequest helper = new BaseTokenRequest() {
            @Override
            protected InnovationRequestBody getInnovationRequestBody() {
                return body;
            }
        };
        return request(context, helper, BaseRequest.PATH + "api/All_Push_Read", responseHandler, listener);
    }

    /**
     * 下载文件
     */
    public static RequestHandle downloadFile(Context context, String url, FileAsyncHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {
        if (!isNetworkConnect(context)) {
            if (listener != null) {
                listener.networkUnavailable();
            }
            return null;
        }
        // 支持重定向
        InnovationClient.getInstance().getClient().getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
        return InnovationClient.getInstance().getClient().get(context, url, responseHandler);
    }

    /**
     * 取消请求
     */
    public static void cancelRequests(Context context, boolean mayInterruptIfRunning) {
        InnovationClient.getInstance().cancelRequests(context, mayInterruptIfRunning);
    }

    private static RequestHandle request(Context context, BaseRequest helper, String path, InnovationHttpResponseHandler responseHandler, NetworkUnavailableListener listener) {

        if (!isNetworkConnect(context)) {
            if (listener != null) {
                listener.networkUnavailable();
            }
            return null;
        }

        HeadInfo.Builder builder = new HeadInfo.Builder(context).os(OS).app(APP).appID(Installation.id(context));
        helper.setHeadInfo(builder.build());
        InnovationClient client = InnovationClient.getInstance();
        client.setSSLSocketFactory();
        return client.post(context, helper.getPathWithHeadInfo(path), helper.getRequestParams(), responseHandler);
    }

    public static boolean isNetworkConnect(Context context) {
        ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED
                || conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED
                || conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTING
                || conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTING) {
            return true;
        } else if (conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.DISCONNECTED
                || conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.DISCONNECTED) {
            return false;
        }
        return false;
    }
}
